package com.zkingsoft.newdao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.zkingsoft.model.Dictionary;
import com.zkingsoft.model.Member;
import com.zkingsoft.plugin.util.DBHelper;
import com.zkingsoft.plugin.util.DBUtil;



public class MemberDao{

	public Member selectById(int id,Dictionary dict){
		Member member = null;
		Connection conn = DBHelper.getConnection(dict);
		String sql = "select * from member where ID = ?";
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			ResultSet rs = ps.executeQuery();
			if(rs.next()){
				member = new Member();
				member.setId(rs.getInt("ID"));
				member.setTel(rs.getString("TEL"));
				member.setScore(rs.getInt("SCORE"));
				member.setRewardscore(rs.getInt("REWARD_SCORE"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DBHelper.closeConnection(conn);
		}
		return member;
	}
	
	
	public Member selectByTel(String tel,Dictionary dict){
		Member member = null;
		Connection conn = DBHelper.getConnection(dict);
		String sql = "select * from member where TEL = ?";
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, tel);
			ResultSet rs = ps.executeQuery();
			if(rs.next()){
				member = new Member();
				member.setId(rs.getInt("ID"));
				member.setTel(rs.getString("TEL"));
				member.setScore(rs.getInt("SCORE"));
				member.setRewardscore(rs.getInt("REWARD_SCORE"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DBHelper.closeConnection(conn);
		}
		return member;
	}
	
	public int updateScoreById(int id,int outMoney,Connection conn)throws Exception{
		String sql = "update member set SCORE = ? where ID = ?";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1, outMoney);
		ps.setInt(2, id);
		int result = ps.executeUpdate();
		if(result>0){
			return 1;
		}
		return 0;
	}
	
	public int updateOtherScoreById(int id,int outMoney,Connection conn)throws Exception{
		String sql = "update member set SCORE = 0,REWARD_SCORE = ? where ID = ?";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1,outMoney);
		ps.setInt(2,id);
		int result = ps.executeUpdate();
		if(result>0){
			return 1;
		}
		return 0;
	}
	
}